package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.dto.UltraGroupSwitchDTO;
import com.yami.shop.bean.model.MeltCloudGroup;
import com.yami.shop.bean.model.MeltCloudUltraGroupApply;
import com.yami.shop.bean.model.MeltCloudUltraGroupRecord;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.vo.UltraGroupApplyVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.security.multishop.model.YamiShopUser;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.*;
import com.yami.shop.service.impl.RongYunGroupServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/shop/ultra/group/apply")
@Tag(name = "融云超级群申请管理")
@AllArgsConstructor
public class MeltCloudUltraGroupApplyController {

    private final MeltCloudUltraGroupApplyService ultraGroupApplyService;
    
    private final MeltCloudUltraGroupRecordService ultraGroupRecordService;
    
    private final ShopDetailService shopDetailService;
    
    private final MeltCloudGroupService groupService;
    
    private final RongYunGroupServiceImpl rongYunGroupService;

    private final SysConfigService sysConfigService;


    @GetMapping("/page")
    @Operation(summary =  "超级群申请列表")
    public ServerResponseEntity<IPage<UltraGroupApplyVO>> page(PageParam<UltraGroupApplyVO> pageParam, @ParameterObject UltraGroupApplyVO applyVO){
        Long shopId = SecurityUtils.getShopUser().getShopId();
        applyVO.setType(1);
        applyVO.setShopId(shopId);
        IPage<UltraGroupApplyVO> page = ultraGroupApplyService.page(pageParam, applyVO);
        return ServerResponseEntity.success(page);
    }
    
    @GetMapping("/getById")
    @Operation(summary =  "超级群申请列表")
    public ServerResponseEntity<MeltCloudUltraGroupApply> apply(@RequestParam Long applyId){
        MeltCloudUltraGroupApply apply = ultraGroupApplyService.getById(applyId);
        return ServerResponseEntity.success(apply);
    }
    
    @PostMapping("/add")
    @Operation(summary =  "新增超级群申请")
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity add(@RequestBody @Valid MeltCloudUltraGroupApply apply){
        YamiShopUser shopUser = SecurityUtils.getShopUser();
        Long shopId = shopUser.getShopId();
        ShopDetail shopDetail = shopDetailService.getById(shopId);
        if (StringUtils.isEmpty(shopDetail.getApiUser()) || StringUtils.isEmpty(shopDetail.getApiPassword())) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "请先充值gas值！");
        }
        apply.setType(1);
        apply.setObjId("shop_" + shopUser.getUserId());
        apply.setShopId(shopId);
        UltraGroupSwitchDTO ultraGroupSwitch = sysConfigService.getSysConfigObject(Constant.ULTRA_GROUP_SWITCH, UltraGroupSwitchDTO.class);
        if (!ultraGroupSwitch.getShopAudit()) {
            // 免审核-扣减gas-创建
            apply.setStatus(3);
            ultraGroupApplyService.save(apply);
            rongYunGroupService.reduceGasAndCreateGroup(apply, true);
        } else {
            ultraGroupApplyService.save(apply);
        }
        return ServerResponseEntity.success();
    }
    
    @PostMapping("/edit")
    @Operation(summary =  "新增超级群申请")
    public ServerResponseEntity update(@RequestBody @Valid MeltCloudUltraGroupApply apply){
        ultraGroupApplyService.updateById(apply);
        return ServerResponseEntity.success();
    }

    @GetMapping("/record/list")
    @Operation(summary =  "超级群记录列表")
    public ServerResponseEntity<List<MeltCloudUltraGroupRecord>> getListByApplyId(@RequestParam Long groupApplyId){
        LambdaQueryWrapper<MeltCloudUltraGroupRecord> eq = Wrappers.lambdaQuery(MeltCloudUltraGroupRecord.class).eq(MeltCloudUltraGroupRecord::getGroupApplyId, groupApplyId).orderByAsc(MeltCloudUltraGroupRecord::getCreateTime);
        List<MeltCloudUltraGroupRecord> list = ultraGroupRecordService.list(eq);
        return ServerResponseEntity.success(list);
    }
    
    
    @PostMapping("/renew")
    @Operation(summary =  "超级群续费")
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity renew(@RequestBody MeltCloudUltraGroupApply apply){
        if (apply.getStatus() != 3)
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "该群未成功创建");
        MeltCloudGroup group = groupService.getById(apply.getGroupId());
        if (!group.getType().equals("1"))
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "只有超级群需要续费");
        if (group.getIsDismiss().equals("1"))
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "该群已解散");
        if (Objects.isNull(apply.getCostId())) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "请选择超级群资费套餐！");
        }
        apply.setName(group.getGroupName());
        apply.setPic(group.getPics());
        apply.setIsOfficial(group.getIsOfficial());
        rongYunGroupService.reduceGasAndCreateGroup(apply, false);
        return ServerResponseEntity.success();
    }


    @GetMapping("/cancel/renewal")
    @Operation(summary =  "取消自动续费")
    public ServerResponseEntity cancelAutoRenewal(@RequestParam Long groupId) {
        return rongYunGroupService.cancelAutoRenewal(groupId);
    }
    
    
    
}
